package com.example.sunsg.okhttptest;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.breadtrip.R;
import java.lang.ref.WeakReference;

public class BaseActivityActivity extends AppCompatActivity {
    private ViewHandler mHandler;

    public static void launch(Context context,Class clazz){
        Intent intent = new Intent(context,clazz);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_activity);
    }

    protected void initHandler(){
        mHandler = new ViewHandler(this);
    }

    protected static final class ViewHandler extends Handler {

        private final WeakReference<BaseActivityActivity> mWeakReference;

        public ViewHandler(BaseActivityActivity holder) {
            mWeakReference = new WeakReference<BaseActivityActivity>(holder);
        }

        @Override
        public void handleMessage(Message msg) {
            if (mWeakReference != null && mWeakReference.get() != null && msg != null) {
                mWeakReference.get().handleMessage(msg);
            }
        }
    }

    protected void handleMessage(Message msg) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mHandler != null){
            mHandler.removeCallbacksAndMessages(null);
        }
    }
}
